{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Squares of a Sorted Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #two-pointers #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #双指针 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: sortedSquares"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #有序数组的平方"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个按 <strong>非递减顺序</strong> 排序的整数数组 <code>nums</code>，返回 <strong>每个数字的平方</strong> 组成的新数组，要求也按 <strong>非递减顺序</strong> 排序。</p>\n",
    "\n",
    "<ul>\n",
    "</ul>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-4,-1,0,3,10]\n",
    "<strong>输出：</strong>[0,1,9,16,100]\n",
    "<strong>解释：</strong>平方后，数组变为 [16,1,0,9,100]\n",
    "排序后，数组变为 [0,1,9,16,100]</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-7,-3,2,3,11]\n",
    "<strong>输出：</strong>[4,9,9,49,121]\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code><span>1 <= nums.length <= </span>10<sup>4</sup></code></li>\n",
    "\t<li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li>\n",
    "\t<li><code>nums</code> 已按 <strong>非递减顺序</strong> 排序</li>\n",
    "</ul>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>进阶：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li>请你<span style=\"color: rgb(36, 41, 46); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Helvetica, Arial, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;\">设计时间复杂度为 <code>O(n)</code> 的算法解决本问题</span></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [squares-of-a-sorted-array](https://leetcode.cn/problems/squares-of-a-sorted-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [squares-of-a-sorted-array](https://leetcode.cn/problems/squares-of-a-sorted-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[-4,-1,0,3,10]', '[-7,-3,2,3,11]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        return sorted([num **2 for num in nums])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        temp=[abs(j) for j in A]\n",
    "        temp.sort()\n",
    "        return [x**2 for x  in temp]\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        \n",
    "        def merge_sort(unsorted):\n",
    "            size = len(unsorted)\n",
    "            if size == 1:\n",
    "                return unsorted\n",
    "            middle = size // 2\n",
    "            a = unsorted[:middle]\n",
    "            b = unsorted[middle:]\n",
    "            a = merge_sort(a)\n",
    "            b = merge_sort(b)\n",
    "            #进行merge\n",
    "            result = []\n",
    "            while a != [] and b != []:\n",
    "                if a[0] < b[0]:\n",
    "                    result.append(a[0])\n",
    "                    a.pop(0)\n",
    "                else:\n",
    "                    result.append(b[0])\n",
    "                    b.pop(0)\n",
    "            result.extend(a)\n",
    "            result.extend(b)\n",
    "            return result\n",
    "\n",
    "                \n",
    "        for i in range(len(A)):\n",
    "            if A[i] < 0: A[i] = - A[i]\n",
    "        \n",
    "       \n",
    "        A = merge_sort(A)\n",
    " \n",
    "        return [x*x for x in A]\n",
    "        \n",
    "        \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        return [number**2 for number in sorted(map(abs,A))]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: 'List[int]') -> 'List[int]':\n",
    "        ret = []\n",
    "        for a in A:\n",
    "            ret.append(a**2)\n",
    "        def quickSort(array):\n",
    "            if len(array) < 2:\n",
    "                return array\n",
    "            else:\n",
    "                less = []\n",
    "                greater = []\n",
    "                equal = []\n",
    "                for a in array:\n",
    "                    if a < array[0]:\n",
    "                        less.append(a)\n",
    "                    elif a > array[0]:\n",
    "                        greater.append(a)\n",
    "                    else:\n",
    "                        equal.append(a)\n",
    "                return quickSort(less) + equal + quickSort(greater)\n",
    "        ret.sort()\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        for i in range(len(A)):\n",
    "            A[i]=A[i]*A[i]\n",
    "        \n",
    "        A.sort()\n",
    "        \n",
    "        return A\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: 'List[int]') -> 'List[int]':\n",
    "        for i,j in enumerate(A):\n",
    "            A[i]=j*j\n",
    "        A.sort()\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        for i in range(0,len(A)):\n",
    "            A[i] = pow(A[i],2)\n",
    "        return sorted(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        l=len(A)\n",
    "        b=[]\n",
    "        for i in range(l):\n",
    "            A[i]=A[i]*A[i]\n",
    "            b.append(A[i])\n",
    "            b.sort()\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        absmin = -1\n",
    "        length = len(A)\n",
    "        for i in range(length):\n",
    "            if A[i] >= 0 and absmin == -1: absmin = i\n",
    "            A[i] = A[i]**2\n",
    "        if absmin == -1: absmin = length - 1\n",
    "        result = []\n",
    "        i = absmin\n",
    "        j = i-1\n",
    "        while j >= 0 and i < length:\n",
    "            if A[j] > A[i]:\n",
    "                result.append(A[i])\n",
    "                i += 1\n",
    "            else:\n",
    "                result.append(A[j])\n",
    "                j -= 1\n",
    "        while j >= 0:\n",
    "            result.append(A[j])\n",
    "            j -= 1\n",
    "        while i < length:\n",
    "            result.append(A[i])\n",
    "            i += 1\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        squares = []\n",
    "        for i in A:\n",
    "            squares.append(i*i)\n",
    "        return sorted(squares)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        return sorted(map(lambda x: pow(x,2), A))\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        ans=[0]*len(A)\n",
    "        l,r=0,len(A)-1\n",
    "        j=r\n",
    "        while l<=r:\n",
    "            le,ri=A[l]*A[l],A[r]*A[r]\n",
    "            if le<ri:\n",
    "                ans[j]=ri\n",
    "                r=r-1\n",
    "            else:\n",
    "                ans[j]=le\n",
    "                l=l+1\n",
    "            j=j-1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: List[int]) -> List[int]:\n",
    "        def f(a):\n",
    "            return pow(a,2)\n",
    "        return sorted(map(f,A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: List[int]) -> List[int]:\n",
    "        answersheet = []\n",
    "        for i in A:\n",
    "            answersheet.append(i*i)\n",
    "            answersheet.sort()\n",
    "        return answersheet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: List[int]\n",
    "        \"\"\"\n",
    "        my_array = []\n",
    "        for i in A:\n",
    "            my_array.append(i ** 2)\n",
    "        print(my_array)\n",
    "        my_array.sort()\n",
    "        return my_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: List[int]) -> List[int]:\n",
    "        for i in range(len(A)):\n",
    "            A[i] *= A[i]\n",
    "        A.sort()\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: List[int]) -> List[int]:\n",
    "        for i in range(len(A)):\n",
    "            A[i] = A[i] * A[i]\n",
    "        A.sort()\n",
    "        return A  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: List[int]) -> List[int]:\n",
    "        for i,j in enumerate(A):\n",
    "            A[i] = j**2\n",
    "        A.sort()\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: List[int]) -> List[int]:\n",
    "        for i in range(len(A)):\n",
    "            A[i]=A[i]*A[i]\n",
    "        A.sort()\n",
    "        return A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = nums[i]**2\n",
    "        nums.sort()\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] *= nums[i]\n",
    "        nums.sort()\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        res = []\n",
    "        lens = len(nums)\n",
    "        for i in range(lens):\n",
    "            res.append(nums[i]**2)\n",
    "        res.sort()\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        res = [x*x for x in nums]\n",
    "        return sorted(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        return sorted([l * l for l in nums])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, A: List[int]) -> List[int]:\n",
    "        ans = []\n",
    "        for i in A:\n",
    "            ans.append(i*i)\n",
    "        return sorted(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:        \n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = nums[i]**2\n",
    "        nums.sort()\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = nums[i] ** 2\n",
    "        nums.sort()\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        for i in range (len(nums)):    #通过下标访问, len(nums)\n",
    "            nums[i] = pow(nums[i], 2)\n",
    "\n",
    "        nums.sort()\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        for i in range (len(nums)):    #通过下标访问, len(nums)\n",
    "            nums[i] = pow(nums[i], 2)\n",
    "\n",
    "        H = 0\n",
    "        T = len(nums) - 1\n",
    "        F = T\n",
    "        new_nums = [0]*len(nums) #事先声明该数组\n",
    "\n",
    "        while H <= T :\n",
    "            if nums[H] > nums[T]:\n",
    "                new_nums[F] = nums[H]\n",
    "                F -= 1\n",
    "                H +=1\n",
    "            else:\n",
    "                new_nums[F] = nums[T]\n",
    "                F -= 1\n",
    "                T -= 1\n",
    "\n",
    "        return new_nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            nums[i] = nums[i]*nums[i]\n",
    "        res = [0 for _ in range(n)]\n",
    "        left = 0\n",
    "        right = n-1\n",
    "        for i in range(n):\n",
    "            if nums[left]<=nums[right]:\n",
    "                res[n-i-1]=nums[right]\n",
    "                right = right-1\n",
    "            else:\n",
    "                res[n-i-1]=nums[left]\n",
    "                left = left+1\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        nums = [x**2 for x in nums]\n",
    "        nums = sorted(nums)\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)):\n",
    "            nums[i]*=nums[i]\n",
    "        nums.sort()\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)):\n",
    "            nums[i]*=nums[i]\n",
    "        nums.sort()\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\r\n",
    "        for i in range(len(nums)):\r\n",
    "            nums[i]*=nums[i]\r\n",
    "        nums.sort()\r\n",
    "    \r\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        for i in range(len(nums)):\n",
    "            nums[i] = nums[i]*nums[i]\n",
    "        nums.sort(reverse=False)\n",
    "        return nums\n",
    "        length = len(nums)\n",
    "        res = [0]*length\n",
    "        l,r = 0,len(nums)-1\n",
    "        while l<=r:\n",
    "            if nums[l]*nums[l]>nums[r]*nums[r]:\n",
    "                res[length-1] = nums[l]*nums[l]\n",
    "                l+=1\n",
    "            else:\n",
    "                res[length-1] = nums[r]*nums[r]\n",
    "                r-=1\n",
    "            length-=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def sortedSquares(self, nums: List[int]) -> List[int]:\n",
    "        size = len(nums)\n",
    "        if size == 1:\n",
    "            return [nums[0]*nums[0]]\n",
    "        left, right = 0, size - 1\n",
    "        tmp = []\n",
    "        while left <= right:\n",
    "            if abs(nums[left]) < abs(nums[right]):\n",
    "                tmp.append(nums[right] * nums[right])\n",
    "                right -= 1\n",
    "            else:\n",
    "                tmp.append(nums[left] * nums[left])\n",
    "                left += 1\n",
    "        new = []\n",
    "        for i in reversed(range(size)):\n",
    "            new.append(tmp[i])\n",
    "        \n",
    "        return new"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
